<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/30/030
 * Time: 12:30
 */

namespace App\Lib\Redis;


use EasySwoole\Component\Singleton;
use EasySwoole\EasySwoole\Config;

class Redis
{
    use Singleton;

    private function __construct()
    {
        if(!extension_loaded('redis'))
        {
            throw new \Exception('redis.so文件不存在');
        }
        try
        {
            $redisCongfig = Config::getInstance()->getConf('REDIS');
            $this->redis = new \Redis;
            $result = $this->redis->connect($redisCongfig['host'],$redisCongfig['port'],$redisCongfig['time_out']);
        }catch (\Exception $e)
        {
            throw new \Exception('redis服务异常');
        }
        if($result === false)
        {
            throw new \Exception('redis连接失败');
        }
    }

    /**
     * @param $key
     * @return bool|string
     */
    public function get($key)
    {
        if(empty($key))
        {
            return '';
        }
        return $this->redis->get($key);
    }
}